package com.af.community.smart.business

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.*
import com.af.community.smart.viewmodel.ContactViewModel
import kotlinx.android.synthetic.main.activity_contact.*
import kotlinx.android.synthetic.main.layout_empty.*
import net.idik.lib.slimadapter.SlimAdapter
import org.koin.androidx.viewmodel.ext.android.getViewModel

class ContactActivity : BaseActivity() {

    private val letters by lazy {
        listOf(
            "A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U",
            "V", "W", "X", "Y", "Z", "#"
        )
    }
    private val list = ArrayList<CommonData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contact)
        initTitle(title = "通讯录")

        getViewModel().getContactList(getString("userId"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    list.apply {
                        clear()
                        addItems(it)
                    }

                    getViewModel().seperateLists(list)
                    mAdapter.updateData(list)
                },
                onFinally = {
                    empty_view.apply { if (list.isEmpty()) visible() else gone() }
                }
            )
    }

    override fun getViewModel() = getViewModel<ContactViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        empty_hint.text = "暂无相关联系人信息！"

        contact_list.apply {
            linearLayoutManager = LinearLayoutManager(baseContext)
            layoutManager = linearLayoutManager

            addItemDecoration(object : NormalDecoration() {
                override fun getHeaderName(pos: Int): String = list[pos].letter
            }.apply {
                setHeaderContentColor(getColorEx(R.color.header))
                setHeaderHeight(dp2px(30f))
                setTextSize(sp2px(12f))
                setTextColor(Color.parseColor("#5D6170"))
            })
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_contact_list) { data, injector ->

                val index = list.indexOf(data)
                val isLast = index == list.size - 1

                injector.text(R.id.item_contact_name, data.userName)
                    .text(R.id.item_notice_type, data.name)
                    .text(R.id.item_notice_time, data.phone)
                    .visibility(
                        R.id.item_contact_divider,
                        if ((!isLast && data.letter != list[index + 1].letter)
                            || isLast
                        ) View.GONE else View.VISIBLE
                    )

                    .clicked(R.id.item_contact) {
                        if (!data.phone.isNullOrEmpty()) {
                            makeCall(data.phone!!)
                        }
                    }
            }
            .attachTo(contact_list)

        contact_index.setIndexBarHeightRatio(0.9f)
        contact_index.indexBar.apply {
            setNorTextColor(getColorEx(R.color.colorAccent))
            setSelTextColor(getColorEx(R.color.blue_dark))
            setIndexTextSize(sp2px(10f))
            setIndexsList(letters)
            setIndexChangeListener { name ->
                if (list.any { it.letter == name }) {
                    val index = list.indexOfFirst { it.letter == name }
                    linearLayoutManager.scrollToPositionWithOffset(index, 0)
                    return@setIndexChangeListener
                }
            }
        }
    }

}
